﻿using Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using ModelDto;
using Service.BaseService;
using System.Reflection;

namespace AssetManegement.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CeShiController : ControllerBase
    {
        private readonly IHubContext<MyHub> _hubContext;
        private readonly MailKitEmailService _emailService;
        private readonly IExcelHelper exportHelper;
        public CeShiController(IExcelHelper exportHelper, IHubContext<MyHub> _hubContext)
        {
            this.exportHelper = exportHelper;
            _emailService = new MailKitEmailService(
                                server: "smtp.qiye.aliyun.com", // SMTP服务器地址
                                port: 25, // SMTP服务器端口
                                enableSsl: false, // 是否启用SSL
                                username: "mes.xn@jpond.com.cn", // SMTP服务器用户名
                                password: "" // SMTP服务器密码
                            );
            this._hubContext = _hubContext;
        }
        [HttpPost]
        public async void Signlar()
        {
            await _hubContext.Clients.All.SendAsync("ReceiveMessage", "测试消息");
        }
        [HttpPost]
        public bool Ceshi()
        {
            // 发件人邮箱和密码
            string fromAddress = "mes.xn@jpond.com.cn";
            //string fromAddress = "zengqinhai@sotrun.com";
            string fromPassword = "";

            // 收件人邮箱
            string toAddress = "865933724@qq.com";

            // 邮件内容
            string subject = "测试内容";
            string body = "<html><body><h1>邮件内容</h1><p>这是邮件正文。</p></body></html>";

            // 发送邮件
            EmailService.SendEmail(fromAddress, fromPassword, "smtp.qiye.aliyun.com", 25, false, toAddress, subject, body);
            return true;
        }

        [HttpPost]
        public async Task<IActionResult> Ceshi2()
        {
            await _emailService.SendEmailAsync(
                from: "mes.xn@jpond.com.cn",
                to: "865933724@qq.com",
                subject: "测试发送",
                body: "测试发送11111",
                isHtml: true
             );

            return Ok();
        }
        /// <summary>
        /// 测试导出功能
        /// </summary>
        /// <returns></returns>

        [HttpPost]
        public ActionResult Export()
        {
            string className = "MyNamespace.MyClass";
            string methodName = "MyMethod";

            // 根据类名获取Type
            Type classType = Type.GetType(className);
            if (classType != null)
            {
                //实例化类
                object instance = Activator.CreateInstance(classType);
                //获取类下面的MyMethod方法
                MethodInfo methodInfo = classType.GetMethod(methodName);
                if (methodInfo != null)
                {
                    //实例化的类调用MyMethod方法，第二个参数是方法的参数数组
                    methodInfo.Invoke(instance, null);
                }
            }



            List<SupplierExport> supplierExports = new List<SupplierExport>()
            {
                new SupplierExport()
                {
                    Code="ceshi1",
                    Name = "测试1",
                    wan = 1,
                    wan1=true,
                    wan2=null,
                    wan3=null,
                    wan4 = DateTime.Now,
                    wan5 = 1.11M,

                },
                new SupplierExport()
                {
                    Code="ceshi2",
                    Name = "测试2",
                    wan = 1,
                    wan1=false,
                    wan2=true,
                    wan3=DateTime.Now,
                    wan4 = DateTime.Now,
                    wan5 = 1.11M,
                }
            };

            var ms = exportHelper.ExportXlsx(supplierExports);


            //测试


            return File(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "测试文件.xlsx");
        }
    }
}
